<?php

namespace Parser\Separator;

use Parser\Enquiry;
use Parser\Parser;
use Parser\Repetition;

//中华
class CIC implements BaseSeparator
{

    public static $ApiKindList = [
        '01' => [
            'code' => '3000',
            'shortName' => '机动车损失险'
        ],
        '02' => [
            'code' => '4000',
            'shortName' => '机动车第三者责任保险',
        ],
        '03' => [
            'code' => '5002',
            'shortName' => '机动车车上人员责任保险(司机)',
        ],
        '04' => [
            'code' => '5001',
            'shortName' => '机动车车上人员责任保险(乘客)',
        ]

//        50012 => '15', //医保外医疗费用责任险（三者险）
    ];

    public function interpreter(array $array, $parser)
    {
        if ($this->error($array, $parser)) {
            return;
        }

        $parser->result = $this->analysis($array);
    }

    public function error(&$array, $parser): bool
    {
        if ($message = $array['compErrorMsg'] ?? false) {
            $parser->message = $message;
            $parser->code = '500';
            return true;
        }
        if ($message = $array['commErrorMsg']?? false) {
            $parser->message = $message;
            $parser->code = '500';
            return true;
        }

        return false;
    }

    public function analysis($array): array
    {
        $enquiry = new Enquiry();

        $enquiry->setVCIDiscount($array['insureDTO']['commPolDTO']['totalDiscount'] ?? '0');

        $enquiry->setTaxTotal($array['insureDTO']['vehTax']['taxTotalAmount'] ?? '0'); 
        $enquiry->setTaxPremium($array['insureDTO']['vehTax']['taxTotalAmount'] ?? '0'); 

        $enquiry->setTCITotal($array['insureDTO']['compPolDTO']['premium'] ?? '0');
        $enquiry->setTCIPremium($array['insureDTO']['compPolDTO']['basePremium'] ?? '0');

        $enquiry->setVCITotal($array['insureDTO']['commPolDTO']['premium'] ?? '0');
        $enquiry->setVCIPremium($array['insureDTO']['commPolDTO']['basePremium'] ?? '0');

        //商业险费率因子

        //自主定价系数
        $enquiry->setIndependentPriceRatio($array['baseInfo']['underFactor']?? '');
        //无赔款优待系数NCD
        $enquiry->setNCDRate($array['baseInfo']['channelFactor'] ?? '0');

        if (isset($array['insureDTO']['commerceList'])) {
            foreach ($array['insureDTO']['commerceList'] as $kindList) {

                $code = self::$ApiKindList[$kindList['riskCode']]['code'] ?? '';
                if (!$code) {
                    continue;
                }
                $coverages['code'] = $code;
                $coverages['shortName'] = self::$ApiKindList[$kindList['riskCode']]['shortName'] ?? '';
                $coverages['insuredDesc'] = (string)$kindList['insuredAmt'] ?? '';
                $coverages['total'] = (string)$kindList['premiumAmt'];
                $enquiry->setVCICoverages($coverages);
            }
        }
        if ($repeat = $array['successMsg'] ?? '') {
            if ($array['result']['totalPremium'] ?? false) {
                $enquiry->success();
            } elseif (preg_match('/重复投保/', $repeat)) {
                $enquiry->failure();
                $enquiry->setErrorTip($repeat);
                return $enquiry->get();
            }
        }
        $enquiry->setTotal();
        $enquiry->success();

        return $enquiry->get();
    }

    public function repetition($array): Repetition
    {
        return new Repetition();
    }
}
